package hehe.json.util;


import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @todo 这里写类名
 * <B>说明：</B>
 * <pre>
 *  略
 * </pre>
 * <B>示例：</B>
 * <pre>
 *  略
 * </pre>
 * <B>日志：</B>
 * <pre>
 *  略
 * </pre>
 * <B>注意事项：</B>
 * <pre>
 *  略
 * </pre>
 */
public class JsonUtil
{
    static Map<String,Method> methods = new HashMap<>();

    public static Object invoke(Object target,String methodName,Object[] args,Class[] argsType) throws Exception
    {
        Class targetClazz = target.getClass();
        Method method;
        String methodKey = targetClazz.getName() + "." + methodName;

        if (methods.containsKey(methodKey)) {
            method = methods.get(targetClazz);
        } else {
            method = targetClazz.getMethod(methodName,argsType);
            methods.put(methodKey,method);
        }

        return method.invoke(target,args);
    }

    /**
     * 执行静态方法
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static Object invoke(Class targetClazz,String methodName,Object[] args,Class[] argsType) throws Exception
    {
        Method method;
        String methodKey = targetClazz.getName() + "." + methodName;

        if (methods.containsKey(methodKey)) {
            method = methods.get(targetClazz);
        } else {
            method = targetClazz.getMethod(methodName,argsType);
            methods.put(methodKey,method);
        }

        return method.invoke(null,args);
    }



    public static Object make(String clazz)
    {
        try {
            Class targetClazz = Class.forName(clazz);
            return targetClazz.newInstance();
        } catch (Exception ex) {
            return null;
        }
    }

    public static Class getClazz(String clazz)
    {
        try {
            return Class.forName(clazz);
        } catch (Exception ex) {
            return null;
        }
    }

}
